//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/*
    2004-05-12          Create by Wei Yunzhi
*/

#include "bulverde.h"
#include "ssp.h"

#define MAKE_COMMAND_WORD(rw, page, addr)         ((unsigned short)((rw<<15) | (page<<11) | (addr<<5)))

// I do not know why: the first time to read register, will get '0xffff' !!!

int TSC_RegRead(unsigned long page, unsigned long addr, unsigned short* data, unsigned long dwNum)
{
    unsigned short frame[SSP_FIFO_DEEP];
    unsigned long i;

    if(dwNum > (SSP_FIFO_DEEP - 2)) return 0;

    frame[0] = MAKE_COMMAND_WORD(1, page, addr);

    for(i=1;i<=dwNum;i++)
    {
        frame[i] = 0;
    }

    if(SSP_Exchange(frame, dwNum+1) != (dwNum+1))
    {
        return(0);
    }

    for(i=0;i<dwNum;i++)
    {
        data[i] = frame[i+1];
    }

    return(1);
}

int TSC_RegWrite(unsigned long page, unsigned long addr, unsigned short* data, unsigned long dwNum)
{
    unsigned short frame[10];
    unsigned long i;

    if(dwNum > (SSP_FIFO_DEEP - 2)) return(0);

    frame[0] = MAKE_COMMAND_WORD(0, page, addr);

    for(i=0;i<dwNum;i++)
    {
        frame[i+1] = data[i];
    }

    return(SSP_Exchange(frame, dwNum+1));
}
